package com.youhome.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youhome.api.dto.*;

import com.youhome.api.dto.SchoolResult;
import com.youhome.service.model.*;
import com.youhome.service.response.InterestPointResult;
import com.youhome.service.service.*;
import com.youhome.utils.GeoUtils;

import com.youhome.utils.StringUtils;
import com.youhome.utils.ZipUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by chen365 on 2018/07/05.
 */
@RestController
@Api(tags = "周边")
@RequestMapping("/homes")
public class CircumController {
    @Autowired
    private ListHubService listHubService;

    @Autowired
    private DownloadService downloadService;

    @Autowired
    private HJZipCodeService hjZipCodeService;

    @Autowired
    private HJCityService hjCityService;

    @Autowired
    private HJStateService hjStateService;

    @Autowired
    private HJSchoolService hjSchoolService;

    @Autowired
    private SolrService solrService;

    @Autowired
    private HomeJunctionService homeJunctionService;

    public static void main(String[] args) {
//       "|"等同于""，空或空的意思
//        正确的写法是
//        "\\|"或者"[|]"
        String group = "zip|city";
        String[] groups = group.split("[|]");
        Arrays.asList(groups).forEach(System.err::println);

        double dist = (double) 200 / 1000;
        System.err.println(dist);

    }


    @ApiOperation(value = "人口年龄结构统计", notes = "人口年龄结构统计", response = PopulationAgeResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/age")
    public Object age(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        PopulationAgeResult populationAgeResult = new PopulationAgeResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编

            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        populationAgeResult.setZip(analysisAge(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        populationAgeResult.setCity(analysisAge(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        populationAgeResult.setState(analysisAge(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, populationAgeResult);
    }


    @ApiOperation(value = "生活成本统计", notes = "生活成本统计", response = CostsResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/costs")
    public Object costs(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        CostsResult costsResult = new CostsResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        costsResult.setZip(analysisCosts(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        costsResult.setCity(analysisCosts(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        costsResult.setState(analysisCosts(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, costsResult);
    }

    @ApiOperation(value = "犯罪数据统计", notes = "犯罪数据统计", response = CrimeResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/crime")
    public Object crime(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        CrimeResult crimeResult = new CrimeResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        crimeResult.setZip(analysisCrime(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        crimeResult.setCity(analysisCrime(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        crimeResult.setState(analysisCrime(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, crimeResult);
    }

    @ApiOperation(value = "教育数据统计", notes = "教育数据统计", response = EducationResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/education")
    public Object education(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        EducationResult educationResult = new EducationResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        educationResult.setZip(analysisEducation(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        educationResult.setCity(analysisEducation(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        educationResult.setState(analysisEducation(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, educationResult);
    }


    @ApiOperation(value = "就业数据统计", notes = "就业数据统计", response = EmploymentResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/employment")
    public Object employment(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        EmploymentResult employmentResult = new EmploymentResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        employmentResult.setZip(analysisEmployment(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        employmentResult.setCity(analysisEmployment(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        employmentResult.setState(analysisEmployment(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, employmentResult);
    }

    @ApiOperation(value = "性别统计", notes = "性别统计", response = GenderResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/gender")
    public Object gender(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        GenderResult genderResult = new GenderResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        genderResult.setZip(analysisGender(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        genderResult.setCity(analysisGender(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        genderResult.setState(analysisGender(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, genderResult);
    }

    @ApiOperation(value = "同屋居住者统计", notes = "同屋居住者统计", response = HouseholdResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/household")
    public Object household(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        HouseholdResult householdResult = new HouseholdResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        householdResult.setZip(analysisHousehold(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        householdResult.setCity(analysisHousehold(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        householdResult.setState(analysisHousehold(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, householdResult);
    }

    @ApiOperation(value = "收入统计", notes = "收入统计", response = IncomeResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/income")
    public Object income(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        IncomeResult incomeResult = new IncomeResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        incomeResult.setZip(analysisIncome(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        incomeResult.setCity(analysisIncome(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        incomeResult.setState(analysisIncome(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, incomeResult);
    }

    @ApiOperation(value = "人口统计", notes = "人口统计", response = PopulationResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/population")
    public Object population(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        PopulationResult populationResult = new PopulationResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        populationResult.setZip(analysisPopulation(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        populationResult.setCity(analysisPopulation(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        populationResult.setState(analysisPopulation(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, populationResult);
    }

    @ApiOperation(value = "族裔统计", notes = "族裔统计", response = RaceResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/race")
    public Object race(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        RaceResult raceResult = new RaceResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());//邮编
            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        raceResult.setZip(analysisRace(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        raceResult.setCity(analysisRace(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        raceResult.setState(analysisRace(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, raceResult);
    }

    @ApiOperation(value = "气候数据统计", notes = "气候数据统计", response = WeatherResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "group", value = "zip|city|state,不填默认zip", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/weather")
    public Object weather(@PathVariable String houseId, String group) {

        String[] groups = initGroups(group);

        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        WeatherResult weatherResult = new WeatherResult();

        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {
            String zip = ZipUtils.parseZip(listHub.getZip());//邮编

            String city = listHub.getCity(); //城市名称
            String state = listHub.getProvince(); //州

            System.err.println(zip + ":" + city + ":" + state);
            if (StringUtils.getPosition(groups, "zip") > -1) {
                if (!StringUtils.isEmpty(zip)) {
                    HJZipCode hjZip = hjZipCodeService.selectByZipCode(zip);
                    if (hjZip != null) {
                        String key = "homejunction/zip/" + hjZip.getId() + ".json";
                        weatherResult.setZip(analysisWeather(key));
                    }
                }
            }

            if (StringUtils.getPosition(groups, "city") > -1) {
                if (!StringUtils.isEmpty(city) && !StringUtils.isEmpty(state)) {
                    String label = city.trim() + ", " + state.trim();
                    HJCity hjCity = hjCityService.selectByLabel(label);
                    if (hjCity != null) {
                        String key = "homejunction/city/" + hjCity.getId() + ".json";
                        weatherResult.setCity(analysisWeather(key));
                    }
                }
            }
            if (StringUtils.getPosition(groups, "state") > -1) {
                if (!StringUtils.isEmpty(state)) {
                    String label = state.trim();
                    HJState hjState = hjStateService.selectByAbbreviation(label);
                    if (hjState != null) {
                        String key = "homejunction/state/" + hjState.getId() + ".json";
                        weatherResult.setState(analysisWeather(key));
                    }
                }
            }

        }
        return new Result(ResultCode.SUCCESS, weatherResult);
    }

    @ApiOperation(value = "周边景点信息", notes = "周边景点信息", response = DTOInterestPointResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "dist", value = "距离(米)", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/poi")
    public Object poi(@PathVariable String houseId, @RequestParam(value = "dist", defaultValue = "1000") Integer dist) {
        if (dist > 1000) {
            dist = 1000;
        }
        long time = System.currentTimeMillis();
        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        List<DTOInterestPointResult> list = new ArrayList<>();
        if (listHub != null) {
            Double lat = listHub.getLatitude(); //纬度
            Double lng = listHub.getLongitude(); //经度
            double distance = (double) dist / 1000; //距离(千米)

            System.err.println("==============dist:" + dist + "==================distance:" + distance + "===============================");
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                List<InterestPointResult> interestPointResults = solrService.selectPoiByCoordinateAndDist(lat, lng, distance);
                ModelMapper modelMapper = new ModelMapper();

                List<DTOInterestPointResult> map =
                        modelMapper.map(interestPointResults, new TypeToken<List<DTOInterestPointResult>>() {
                }.getType());
                return new Result(ResultCode.SUCCESS, map);
            }
        }
        System.err.println("time:" + (System.currentTimeMillis() - time));
        return new Result(ResultCode.SUCCESS, list);
    }

    @ApiOperation(value = "周边景点信息", notes = "周边景点信息", response = DTOInterestPointResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "dist", value = "距离(米)", required = false, paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "density", value = "点与点间距离(米)", required = false, paramType = "query")
    })
    @GetMapping(value = "/{houseId}/poi/density")
    public Object poiDensity(@PathVariable String houseId, @RequestParam(value = "dist", defaultValue = "1000") Integer dist,
                             @RequestParam(value = "density", defaultValue = "1000") Integer density) {
        if (dist > 1000) {
            dist = 1000;
        }
        long time = System.currentTimeMillis();
        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        List<DTOInterestPointResult> list = new ArrayList<>();
        if (listHub != null) {
            Double lat = listHub.getLatitude(); //纬度
            Double lng = listHub.getLongitude(); //经度
            double distance = (double) dist / 1000; //距离(千米)

            System.err.println("==============dist:" + dist + "==================distance:" + distance + "===============================");
            if (!StringUtils.isEmpty(lat) && !StringUtils.isEmpty(lng)) {
                List<InterestPointResult> interestPointResults = solrService.selectPoiByCoordinateAndDist(lat, lng, distance);
                ModelMapper modelMapper = new ModelMapper();
                list = modelMapper.map(interestPointResults, new TypeToken<List<DTOInterestPointResult>>() {
                }.getType());
            }
            List<DTOInterestPointResult> densitys = density(list, density);

            System.err.println("list====>>" + list.size());
            System.err.println("densitys====>>" + densitys.size());
            return new Result(ResultCode.SUCCESS, densitys);
        }
        System.err.println("time:" + (System.currentTimeMillis() - time));
        return new Result(ResultCode.SUCCESS, list);
    }

    @ApiOperation(value = "学校", notes = "学校", response = SchoolResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/{houseId}/schools")
    public Object schools(@PathVariable String houseId) {
        ListHub listHub = listHubService.selectByPrimaryKey(houseId);
        SchoolResult school = new SchoolResult();
        if (listHub != null && !StringUtils.isEmpty(listHub.getZip())) {

            String zip = ZipUtils.parseZip(listHub.getZip());
            List<HJSchool> hjSchools = hjSchoolService.selectByZip(zip);

            List<SchoolItemResult> elementary = new ArrayList<>();
            List<SchoolItemResult> middle = new ArrayList<>();
            List<SchoolItemResult> high = new ArrayList<>();

            if (hjSchools != null && hjSchools.size() > 0) {
                for (HJSchool hjSchool : hjSchools) {
                    SchoolItemResult schoolItem = toSchoolItem(hjSchool);

                    if (hjSchool.getHasElementary() != null && hjSchool.getHasElementary()) {
                        elementary.add(schoolItem);
                    }
                    if (hjSchool.getHasMiddle() != null && hjSchool.getHasMiddle()) {
                        middle.add(schoolItem);
                    }
                    if (hjSchool.getHasHigh() != null && hjSchool.getHasHigh()) {
                        high.add(schoolItem);
                    }
                }
            }

            if (elementary.size() > 0) {
                school.setElementary(elementary);
            }
            if (high.size() > 0) {
                school.setHigh(high);
            }
            if (middle.size() > 0) {
                school.setMiddle(middle);
            }
        }

        return new Result(ResultCode.SUCCESS, school);
    }


    @ApiOperation(value = "学校详情", notes = "学校详情", response = SchoolDetail.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "schoolId", value = "学校ID", required = true, paramType = "path")
    })
    @GetMapping(value = "/schools/{schoolId}")
    public Object schoolDetails(@PathVariable String schoolId) {

        HJSchool hjSchool = hjSchoolService.selectByPrimaryKey(schoolId);
        SchoolDetail schoolDetail = new SchoolDetail();

        if (hjSchool != null) {
            String key = "homejunction/school/" + hjSchool.getId() + ".json";
            String json = downloadService.downloadJsonFromOSS(key);
            if (!StringUtils.isEmpty(json)) {
                JSONObject jsonObject = JSON.parseObject(json);
                if (jsonObject.get("name") != null) {
                    schoolDetail.setName(jsonObject.get("name").toString());
                }

                List<String> grade = new ArrayList<>();
                if (jsonObject.get("gradeLow") != null) {
                    grade.add(jsonObject.get("gradeLow").toString());
                }
                if (jsonObject.get("gradeHigh") != null) {
                    grade.add(jsonObject.get("gradeHigh").toString());
                }

                if (grade.size() > 0) {
                    schoolDetail.setGrade(String.join("-", grade));
                }
                if (jsonObject.get("address") != null) {
                    JSONObject jsonObject_address = JSON.parseObject(jsonObject.get("address").toString());
                    List<String> address = new ArrayList<>();
                    if (jsonObject_address.get("deliveryLine") != null) {
                        address.add(jsonObject_address.get("deliveryLine").toString());
                    }
                    if (jsonObject_address.get("city") != null) {
                        address.add(jsonObject_address.get("city").toString());
                    }
                    if (jsonObject_address.get("state") != null) {
                        address.add(jsonObject_address.get("state").toString());
                    }
                    if (jsonObject_address.get("zip") != null) {
                        address.add(jsonObject_address.get("zip").toString());
                    }
                    if (address.size() > 0) {
                        schoolDetail.setAddress(String.join("-", address));
                    }
                }

                if (jsonObject.get("schoolType") != null) {
                    schoolDetail.setType(jsonObject.get("schoolType").toString());
                }
                if (jsonObject.get("admission") != null) {
                    schoolDetail.setAdmission(jsonObject.get("admission").toString());
                }
                if (jsonObject.get("phone") != null) {
                    schoolDetail.setPhone(jsonObject.get("phone").toString());
                }
                if (jsonObject.get("url") != null) {
                    schoolDetail.setUrl(jsonObject.get("url").toString());
                }
                if (jsonObject.get("advancedPlacement") != null) {
                    schoolDetail.setAdvanced_placement(Boolean.valueOf(jsonObject.get("advancedPlacement").toString()));
                }
                if (jsonObject.get("charter") != null) {
                    schoolDetail.setCharter(Boolean.valueOf(jsonObject.get("charter").toString()));
                }
                if (jsonObject.get("charter") != null) {
                    schoolDetail.setCharter(Boolean.valueOf(jsonObject.get("charter").toString()));
                }
                if (jsonObject.get("expenditure") != null) {
                    schoolDetail.setExpenditure(Double.valueOf(jsonObject.get("expenditure").toString()));
                }
                if (jsonObject.get("library") != null) {
                    schoolDetail.setLibrary(Boolean.valueOf(jsonObject.get("library").toString()));
                }
                if (jsonObject.get("principal") != null) {
                    schoolDetail.setPrincipal(jsonObject.get("principal").toString());
                }
                if (jsonObject.get("gsRating") != null) {
                    schoolDetail.setRating(jsonObject.get("gsRating").toString());
                }
                if (jsonObject.get("totalStudents") != null) {
                    schoolDetail.setTotal_students(Double.valueOf(jsonObject.get("totalStudents").toString()));
                }
                if (jsonObject.get("totalTeachers") != null) {
                    schoolDetail.setTotal_teachers(Double.valueOf(jsonObject.get("totalTeachers").toString()));
                }
                if (jsonObject.get("studentTeacherRatio") != null) {
                    schoolDetail.setStudent_teacher_ratio(jsonObject.get("studentTeacherRatio").toString());
                }
                if (jsonObject.get("demographics") != null) {
                    JSONObject jsonObject_demographics = JSON.parseObject(jsonObject.get("demographics").toString());
                    RaceItemResult race = new RaceItemResult();

                    if (jsonObject_demographics.get("asian") != null) {
                        race.setAsian(Double.valueOf(jsonObject_demographics.get("asian").toString()));
                    }
                    if (jsonObject_demographics.get("black") != null) {
                        race.setBlack(Double.valueOf(jsonObject_demographics.get("black").toString()));
                    }
                    if (jsonObject_demographics.get("hispanic") != null) {
                        race.setHispanic(Double.valueOf(jsonObject_demographics.get("hispanic").toString()));
                    }
                    if (jsonObject_demographics.get("white") != null) {
                        race.setWhite(Double.valueOf(jsonObject_demographics.get("white").toString()));
                    }
                    if (jsonObject_demographics.get("pacific") != null) {
                        race.setPacific(Double.valueOf(jsonObject_demographics.get("pacific").toString()));
                    }
                    if (jsonObject_demographics.get("native") != null) {
                        race.setNatives(Double.valueOf(jsonObject_demographics.get("native").toString()));
                    }
                    schoolDetail.setRace(race);

                    GenderItemResult gender = new GenderItemResult();
                    if (jsonObject_demographics.get("male") != null) {
                        gender.setMale(Double.valueOf(jsonObject_demographics.get("male").toString()));
                    }
                    if (jsonObject_demographics.get("female") != null) {
                        gender.setFemale(Double.valueOf(jsonObject_demographics.get("female").toString()));
                    }
                    schoolDetail.setGender(gender);
                }
                if (jsonObject.get("enrollment") != null) {
                    JSONObject jsonObject_enrollment = JSON.parseObject(jsonObject.get("enrollment").toString());
                    Enrollment enrollment = new Enrollment();
                    if (jsonObject_enrollment.get("pk") != null) {
                        enrollment.setPk(Integer.valueOf(jsonObject_enrollment.get("pk").toString()));
                    }
                    if (jsonObject_enrollment.get("kg") != null) {
                        enrollment.setKg(Integer.valueOf(jsonObject_enrollment.get("kg").toString()));
                    }
                    if (jsonObject_enrollment.get("1") != null) {
                        enrollment.setY_1(Integer.valueOf(jsonObject_enrollment.get("1").toString()));
                    }
                    if (jsonObject_enrollment.get("2") != null) {
                        enrollment.setY_2(Integer.valueOf(jsonObject_enrollment.get("2").toString()));
                    }
                    if (jsonObject_enrollment.get("3") != null) {
                        enrollment.setY_3(Integer.valueOf(jsonObject_enrollment.get("3").toString()));
                    }
                    if (jsonObject_enrollment.get("4") != null) {
                        enrollment.setY_4(Integer.valueOf(jsonObject_enrollment.get("4").toString()));
                    }
                    if (jsonObject_enrollment.get("5") != null) {
                        enrollment.setY_5(Integer.valueOf(jsonObject_enrollment.get("5").toString()));
                    }
                    if (jsonObject_enrollment.get("6") != null) {
                        enrollment.setY_6(Integer.valueOf(jsonObject_enrollment.get("6").toString()));
                    }
                    if (jsonObject_enrollment.get("7") != null) {
                        enrollment.setY_7(Integer.valueOf(jsonObject_enrollment.get("7").toString()));
                    }
                    if (jsonObject_enrollment.get("8") != null) {
                        enrollment.setY_8(Integer.valueOf(jsonObject_enrollment.get("8").toString()));
                    }
                    if (jsonObject_enrollment.get("9") != null) {
                        enrollment.setY_9(Integer.valueOf(jsonObject_enrollment.get("9").toString()));
                    }
                    if (jsonObject_enrollment.get("10") != null) {
                        enrollment.setY_10(Integer.valueOf(jsonObject_enrollment.get("10").toString()));
                    }
                    if (jsonObject_enrollment.get("11") != null) {
                        enrollment.setY_11(Integer.valueOf(jsonObject_enrollment.get("11").toString()));
                    }
                    if (jsonObject_enrollment.get("12") != null) {
                        enrollment.setY_12(Integer.valueOf(jsonObject_enrollment.get("12").toString()));
                    }

                    schoolDetail.setEnrollment(enrollment);
                }

            }

        }
        return new Result(ResultCode.SUCCESS, schoolDetail);
    }


    /**
     * 解析人口年龄结构
     *
     * @param key
     * @return
     */
    public PopulationAgeItemResult analysisAge(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        PopulationAgeItemResult zipPopulationAgeItemResult = new PopulationAgeItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("age") != null) {
                JSONObject jsonObject_age = JSON.parseObject(jsonObject.get("age").toString());
                //族裔平均年龄
                RaceItemResult raceResult = new RaceItemResult();
                if (jsonObject_age.get("white") != null) {
                    raceResult.setWhite(Double.valueOf(jsonObject_age.get("white").toString()));
                }
                if (jsonObject_age.get("black") != null) {
                    raceResult.setBlack(Double.valueOf(jsonObject_age.get("black").toString()));
                }
                if (jsonObject_age.get("asian") != null) {
                    raceResult.setAsian(Double.valueOf(jsonObject_age.get("asian").toString()));
                }
                if (jsonObject_age.get("hispanic") != null) {
                    raceResult.setHispanic(Double.valueOf(jsonObject_age.get("hispanic").toString()));
                }
                zipPopulationAgeItemResult.setRace(raceResult);

                //年龄组百分比
                DTOAgeResult DTOAgeResult = new DTOAgeResult();
                if (jsonObject_age.get("Y0_5") != null) {
                    DTOAgeResult.setY0_5(Double.valueOf(jsonObject_age.get("Y0_5").toString()));
                }
                if (jsonObject_age.get("Y6_11") != null) {
                    DTOAgeResult.setY6_11(Double.valueOf(jsonObject_age.get("Y6_11").toString()));
                }
                if (jsonObject_age.get("Y12_17") != null) {
                    DTOAgeResult.setY12_17(Double.valueOf(jsonObject_age.get("Y12_17").toString()));
                }
                if (jsonObject_age.get("Y18_24") != null) {
                    DTOAgeResult.setY18_24(Double.valueOf(jsonObject_age.get("Y18_24").toString()));
                }
                if (jsonObject_age.get("Y25_34") != null) {
                    DTOAgeResult.setY25_34(Double.valueOf(jsonObject_age.get("Y25_34").toString()));
                }
                if (jsonObject_age.get("Y35_44") != null) {
                    DTOAgeResult.setY35_44(Double.valueOf(jsonObject_age.get("Y35_44").toString()));
                }
                if (jsonObject_age.get("Y45_54") != null) {
                    DTOAgeResult.setY45_54(Double.valueOf(jsonObject_age.get("Y45_54").toString()));
                }
                if (jsonObject_age.get("Y55_64") != null) {
                    DTOAgeResult.setY55_64(Double.valueOf(jsonObject_age.get("Y55_64").toString()));
                }
                if (jsonObject_age.get("Y65_74") != null) {
                    DTOAgeResult.setY65_74(Double.valueOf(jsonObject_age.get("Y65_74").toString()));
                }
                if (jsonObject_age.get("Y75_84") != null) {
                    DTOAgeResult.setY75_84(Double.valueOf(jsonObject_age.get("Y75_84").toString()));
                }
                if (jsonObject_age.get("Y85_UP") != null) {
                    DTOAgeResult.setY85_up(Double.valueOf(jsonObject_age.get("Y85_UP").toString()));
                }
                zipPopulationAgeItemResult.setAge(DTOAgeResult);

                //性别
                GenderItemResult genderItemResult = new GenderItemResult();
                if (jsonObject_age.get("male") != null) {
                    genderItemResult.setMale(Double.valueOf(jsonObject_age.get("male").toString()));
                }
                if (jsonObject_age.get("female") != null) {
                    genderItemResult.setFemale(Double.valueOf(jsonObject_age.get("female").toString()));
                }
                zipPopulationAgeItemResult.setGender(genderItemResult);
                if (jsonObject_age.get("median") != null) {
                    zipPopulationAgeItemResult.setMedian(Double.valueOf(jsonObject_age.get("median").toString()));
                }
            }
        }
        return zipPopulationAgeItemResult;
    }

    /**
     * 解析生活成本
     *
     * @param key
     * @return
     */
    public CostsItemResult analysisCosts(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        CostsItemResult costsItemResult = new CostsItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("costs") != null) {
                JSONObject costs = JSON.parseObject(jsonObject.get("costs").toString());
                if (costs.get("overall") != null) {
                    costsItemResult.setOverall(Double.valueOf(costs.get("overall").toString()));
                }
                if (costs.get("apparel") != null) {
                    costsItemResult.setApparel(Double.valueOf(costs.get("apparel").toString()));
                }
                if (costs.get("education") != null) {
                    costsItemResult.setEducation(Double.valueOf(costs.get("education").toString()));
                }
                if (costs.get("entertainment") != null) {
                    costsItemResult.setEntertainment(Double.valueOf(costs.get("entertainment").toString()));
                }
                if (costs.get("food") != null) {
                    costsItemResult.setFood(Double.valueOf(costs.get("food").toString()));
                }
                if (costs.get("healthcare") != null) {
                    costsItemResult.setHealthcare(Double.valueOf(costs.get("healthcare").toString()));
                }
                if (costs.get("housing") != null) {
                    costsItemResult.setHousing(Double.valueOf(costs.get("housing").toString()));
                }
                if (costs.get("transportation") != null) {
                    costsItemResult.setTransportation(Double.valueOf(costs.get("transportation").toString()));
                }
                if (costs.get("utilities") != null) {
                    costsItemResult.setUtilities(Double.valueOf(costs.get("utilities").toString()));
                }
            }
        }
        return costsItemResult;
    }

    /**
     * 解析犯罪数据
     *
     * @param key
     * @return
     */
    public CrimeItemResult analysisCrime(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        CrimeItemResult crimeItemResult = new CrimeItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("crime") != null) {
                JSONObject crime = JSON.parseObject(jsonObject.get("crime").toString());
                if (crime.get("overall") != null) {
                    crimeItemResult.setOverall(Double.valueOf(crime.get("overall").toString()));
                }
                if (crime.get("assault") != null) {
                    crimeItemResult.setAssault(Double.valueOf(crime.get("assault").toString()));
                }
                if (crime.get("burglary") != null) {
                    crimeItemResult.setBurglary(Double.valueOf(crime.get("burglary").toString()));
                }
                if (crime.get("larceny") != null) {
                    crimeItemResult.setLarceny(Double.valueOf(crime.get("larceny").toString()));
                }
                if (crime.get("murder") != null) {
                    crimeItemResult.setMurder(Double.valueOf(crime.get("murder").toString()));
                }
                if (crime.get("rape") != null) {
                    crimeItemResult.setRape(Double.valueOf(crime.get("rape").toString()));
                }
                if (crime.get("robbery") != null) {
                    crimeItemResult.setRobbery(Double.valueOf(crime.get("robbery").toString()));
                }
                if (crime.get("vehicle") != null) {
                    crimeItemResult.setVehicle(Double.valueOf(crime.get("vehicle").toString()));
                }

            }
        }
        return crimeItemResult;
    }


    /**
     * 解析教育数据
     *
     * @param key
     * @return
     */
    public EducationItemResult analysisEducation(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        EducationItemResult educationItemResult = new EducationItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("education") != null) {
                JSONObject education = JSON.parseObject(jsonObject.get("education").toString());
                if (education.get("highschool") != null) {
                    educationItemResult.setHighschool(Double.valueOf(education.get("highschool").toString()));
                }
                if (education.get("associate") != null) {
                    educationItemResult.setAssociate(Double.valueOf(education.get("associate").toString()));
                }
                if (education.get("bachelor") != null) {
                    educationItemResult.setBachelor(Double.valueOf(education.get("bachelor").toString()));
                }
                if (education.get("graduate") != null) {
                    educationItemResult.setGraduate(Double.valueOf(education.get("graduate").toString()));
                }
            }
        }
        return educationItemResult;
    }


    /**
     * 解析就业数据
     *
     * @param key
     * @return
     */
    public EmploymentItemResult analysisEmployment(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        EmploymentItemResult employmentItemResult = new EmploymentItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("employment") != null) {
                JSONObject employment = JSON.parseObject(jsonObject.get("employment").toString());

                if (employment.get("accommodations") != null) {
                    employmentItemResult.setAccommodations(Double.valueOf(employment.get("accommodations").toString()));
                }
                if (employment.get("administration") != null) {
                    employmentItemResult.setAdministration(Double.valueOf(employment.get("administration").toString()));
                }
                if (employment.get("agriculture") != null) {
                    employmentItemResult.setAgriculture(Double.valueOf(employment.get("agriculture").toString()));
                }
                if (employment.get("arts") != null) {
                    employmentItemResult.setArts(Double.valueOf(employment.get("arts").toString()));
                }
                if (employment.get("construction") != null) {
                    employmentItemResult.setConstruction(Double.valueOf(employment.get("construction").toString()));
                }
                if (employment.get("education") != null) {
                    employmentItemResult.setEducation(Double.valueOf(employment.get("education").toString()));
                }
                if (employment.get("finance") != null) {
                    employmentItemResult.setFinance(Double.valueOf(employment.get("finance").toString()));
                }
                if (employment.get("government") != null) {
                    employmentItemResult.setGovernment(Double.valueOf(employment.get("government").toString()));
                }
                if (employment.get("healthcare") != null) {
                    employmentItemResult.setHealthcare(Double.valueOf(employment.get("healthcare").toString()));
                }
                if (employment.get("management") != null) {
                    employmentItemResult.setManagement(Double.valueOf(employment.get("management").toString()));
                }
                if (employment.get("manufacturing") != null) {
                    employmentItemResult.setManufacturing(Double.valueOf(employment.get("manufacturing").toString()));
                }
                if (employment.get("mining") != null) {
                    employmentItemResult.setMining(Double.valueOf(employment.get("mining").toString()));
                }
                if (employment.get("other") != null) {
                    employmentItemResult.setOther(Double.valueOf(employment.get("other").toString()));
                }
                if (employment.get("professional") != null) {
                    employmentItemResult.setProfessional(Double.valueOf(employment.get("professional").toString()));
                }
                if (employment.get("realestate") != null) {
                    employmentItemResult.setRealestate(Double.valueOf(employment.get("realestate").toString()));
                }
                if (employment.get("retail") != null) {
                    employmentItemResult.setRetail(Double.valueOf(employment.get("retail").toString()));
                }
                if (employment.get("technology") != null) {
                    employmentItemResult.setTechnology(Double.valueOf(employment.get("technology").toString()));
                }
                if (employment.get("transportation") != null) {
                    employmentItemResult.setTransportation(Double.valueOf(employment.get("transportation").toString()));
                }
                if (employment.get("utilities") != null) {
                    employmentItemResult.setUtilities(Double.valueOf(employment.get("utilities").toString()));
                }
                if (employment.get("wholesale") != null) {
                    employmentItemResult.setWholesale(Double.valueOf(employment.get("wholesale").toString()));
                }
            }
        }
        return employmentItemResult;
    }

    /**
     * 解析性别
     *
     * @param key
     * @return
     */
    public GenderItemResult analysisGender(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        GenderItemResult genderItemResult = new GenderItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("gender") != null) {
                JSONObject gender = JSON.parseObject(jsonObject.get("gender").toString());

                if (gender.get("male") != null) {
                    genderItemResult.setMale(Double.valueOf(gender.get("male").toString()));
                }
                if (gender.get("female") != null) {
                    genderItemResult.setFemale(Double.valueOf(gender.get("female").toString()));
                }
            }
        }
        return genderItemResult;
    }

    /**
     * 解析同屋居住者
     *
     * @param key
     * @return
     */
    public HouseholdItemResult analysisHousehold(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        HouseholdItemResult householdItemResult = new HouseholdItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("households") != null) {
                JSONObject households = JSON.parseObject(jsonObject.get("households").toString());

                if (households.get("total") != null) {
                    householdItemResult.setTotal(Double.valueOf(households.get("total").toString()));
                }
                if (households.get("value") != null) {
                    householdItemResult.setValue(Double.valueOf(households.get("value").toString()));
                }
                if (households.get("yearBuilt") != null) {
                    householdItemResult.setYear_built(Double.valueOf(households.get("yearBuilt").toString()));
                }
                if (households.get("duration") != null) {
                    householdItemResult.setDuration(Double.valueOf(households.get("duration").toString()));
                }
                if (households.get("occupancy") != null) {
                    JSONObject occupancy = JSON.parseObject(households.get("occupancy").toString());
                    OccupancyResult occupancyResult = new OccupancyResult();
                    if (occupancy.get("owned") != null) {
                        occupancyResult.setOwned(Double.valueOf(occupancy.get("owned").toString()));
                    }
                    if (occupancy.get("rented") != null) {
                        occupancyResult.setRented(Double.valueOf(occupancy.get("rented").toString()));
                    }
                    if (occupancy.get("vacant") != null) {
                        occupancyResult.setVacant(Double.valueOf(occupancy.get("vacant").toString()));
                    }
                    if (occupancy.get("families") != null) {
                        occupancyResult.setFamilies(Double.valueOf(occupancy.get("families").toString()));
                    }
                    if (occupancy.get("married") != null) {
                        occupancyResult.setMarried(Double.valueOf(occupancy.get("married").toString()));
                    }
                    if (occupancy.get("children") != null) {
                        occupancyResult.setChildren(Double.valueOf(occupancy.get("children").toString()));
                    }
                    householdItemResult.setOccupancy(occupancyResult);
                }
            }
        }
        return householdItemResult;
    }

    /**
     * 解析收入
     *
     * @param key
     * @return
     */
    public IncomeItemResult analysisIncome(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        IncomeItemResult incomeItemResult = new IncomeItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("income") != null) {
                JSONObject income = JSON.parseObject(jsonObject.get("income").toString());

                if (income.get("average") != null) {
                    incomeItemResult.setAverage(Double.valueOf(income.get("average").toString()));
                }
                if (income.get("median") != null) {
                    incomeItemResult.setMedian(Double.valueOf(income.get("median").toString()));
                }
                if (income.get("percent0_15") != null) {
                    incomeItemResult.setPercent0_15(Double.valueOf(income.get("percent0_15").toString()));
                }
                if (income.get("percent15_25") != null) {
                    incomeItemResult.setPercent15_25(Double.valueOf(income.get("percent15_25").toString()));
                }
                if (income.get("percent25_35") != null) {
                    incomeItemResult.setPercent25_35(Double.valueOf(income.get("percent25_35").toString()));
                }
                if (income.get("percent35_50") != null) {
                    incomeItemResult.setPercent35_50(Double.valueOf(income.get("percent35_50").toString()));
                }
                if (income.get("percent50_75") != null) {
                    incomeItemResult.setPercent50_75(Double.valueOf(income.get("percent50_75").toString()));
                }
                if (income.get("percent75_100") != null) {
                    incomeItemResult.setPercent75_100(Double.valueOf(income.get("percent75_100").toString()));
                }
                if (income.get("percent100_125") != null) {
                    incomeItemResult.setPercent100_125(Double.valueOf(income.get("percent100_125").toString()));
                }
                if (income.get("percent125_150") != null) {
                    incomeItemResult.setPercent125_150(Double.valueOf(income.get("percent125_150").toString()));
                }
                if (income.get("percent150_200") != null) {
                    incomeItemResult.setPercent150_200(Double.valueOf(income.get("percent150_200").toString()));
                }
                if (income.get("percent200_up") != null) {
                    incomeItemResult.setPercent200_up(Double.valueOf(income.get("percent200_up").toString()));
                }

            }
        }
        return incomeItemResult;
    }

    /**
     * 解析人口
     *
     * @param key
     * @return
     */
    public PopulationItemResult analysisPopulation(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        PopulationItemResult populationItemResult = new PopulationItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("population") != null) {
                JSONObject population = JSON.parseObject(jsonObject.get("population").toString());

                if (population.get("current") != null) {
                    populationItemResult.setCurrent(Double.valueOf(population.get("current").toString()));
                }
                if (population.get("density") != null) {
                    populationItemResult.setDensity(Double.valueOf(population.get("density").toString()));
                }
                if (population.get("1990") != null) {
                    populationItemResult.setY1990(Double.valueOf(population.get("1990").toString()));
                }
                if (population.get("2000") != null) {
                    populationItemResult.setY2000(Double.valueOf(population.get("2000").toString()));
                }
                if (population.get("2010") != null) {
                    populationItemResult.setY2010(Double.valueOf(population.get("2010").toString()));
                }
            }
        }
        return populationItemResult;
    }

    /**
     * 解析族裔
     *
     * @param key
     * @return
     */
    public RaceItemResult analysisRace(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        RaceItemResult raceItemResult = new RaceItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("race") != null) {
                JSONObject race = JSON.parseObject(jsonObject.get("race").toString());

                if (race.get("white") != null) {
                    raceItemResult.setWhite(Double.valueOf(race.get("white").toString()));
                }
                if (race.get("black") != null) {
                    raceItemResult.setBlack(Double.valueOf(race.get("black").toString()));
                }
                if (race.get("asian") != null) {
                    raceItemResult.setAsian(Double.valueOf(race.get("asian").toString()));
                }
                if (race.get("hispanic") != null) {
                    raceItemResult.setHispanic(Double.valueOf(race.get("hispanic").toString()));
                }
                if (race.get("native") != null) {
                    raceItemResult.setNatives(Double.valueOf(race.get("native").toString()));
                }
            }
        }
        return raceItemResult;
    }


    /**
     * 解析气候数据
     *
     * @param key
     * @return
     */
    public WeatherItemResult analysisWeather(String key) {
        String json = downloadService.downloadJsonFromOSS(key);
        WeatherItemResult weatherItemResult = new WeatherItemResult();
        if (!StringUtils.isEmpty(json)) {
            JSONObject jsonObject = JSON.parseObject(json);
            if (jsonObject.get("weather") != null) {
                JSONObject weather = JSON.parseObject(jsonObject.get("weather").toString());

                if (weather.get("jan") != null) {
                    JSONObject jan = JSON.parseObject(weather.get("jan").toString());
                    Temperature temperature = new Temperature();
                    if (jan.get("low") != null) {
                        temperature.setLow(Double.valueOf(jan.get("low").toString()));
                    }
                    if (jan.get("high") != null) {
                        temperature.setHigh(Double.valueOf(jan.get("high").toString()));
                    }
                    weatherItemResult.setJan(temperature);
                }

                if (weather.get("apr") != null) {
                    JSONObject apr = JSON.parseObject(weather.get("apr").toString());
                    Temperature temperature = new Temperature();
                    if (apr.get("low") != null) {
                        temperature.setLow(Double.valueOf(apr.get("low").toString()));
                    }
                    if (apr.get("high") != null) {
                        temperature.setHigh(Double.valueOf(apr.get("high").toString()));
                    }
                    weatherItemResult.setApr(temperature);
                }

                if (weather.get("jul") != null) {
                    JSONObject jul = JSON.parseObject(weather.get("jul").toString());
                    Temperature temperature = new Temperature();
                    if (jul.get("low") != null) {
                        temperature.setLow(Double.valueOf(jul.get("low").toString()));
                    }
                    if (jul.get("high") != null) {
                        temperature.setHigh(Double.valueOf(jul.get("high").toString()));
                    }
                    weatherItemResult.setJul(temperature);
                }

                if (weather.get("oct") != null) {
                    JSONObject oct = JSON.parseObject(weather.get("oct").toString());
                    Temperature temperature = new Temperature();
                    if (oct.get("low") != null) {
                        temperature.setLow(Double.valueOf(oct.get("low").toString()));
                    }
                    if (oct.get("high") != null) {
                        temperature.setHigh(Double.valueOf(oct.get("high").toString()));
                    }
                    weatherItemResult.setOct(temperature);
                }

                if (weather.get("annual") != null) {
                    JSONObject annual = JSON.parseObject(weather.get("annual").toString());
                    Temperature temperature = new Temperature();
                    if (annual.get("low") != null) {
                        temperature.setLow(Double.valueOf(annual.get("low").toString()));
                    }
                    if (annual.get("high") != null) {
                        temperature.setHigh(Double.valueOf(annual.get("high").toString()));
                    }
                    weatherItemResult.setAnnual(temperature);
                }

                if (weather.get("precipitation") != null) {
                    JSONObject precipitationJson = JSON.parseObject(weather.get("precipitation").toString());
                    Precipitation precipitation = new Precipitation();
                    if (precipitationJson.get("snow") != null) {
                        JSONObject snowJson = JSON.parseObject(precipitationJson.get("snow").toString());
                        Snow snow = new Snow();
                        if (snowJson.get("amount") != null) {
                            snow.setAmount(Double.valueOf(snowJson.get("amount").toString()));
                        }
                        if (snowJson.get("daysPerYear") != null) {
                            snow.setDays_per_year(Integer.valueOf(snowJson.get("daysPerYear").toString()));
                        }
                        precipitation.setSnow(snow);
                    }
                    if (precipitationJson.get("rain") != null) {
                        JSONObject rainJson = JSON.parseObject(precipitationJson.get("rain").toString());
                        Rain rain = new Rain();
                        if (rainJson.get("amount") != null) {
                            rain.setAmount(Double.valueOf(rainJson.get("amount").toString()));
                        }
                        if (rainJson.get("daysPerYear") != null) {
                            rain.setDays_per_year(Integer.valueOf(rainJson.get("daysPerYear").toString()));
                        }
                        precipitation.setRain(rain);
                    }
                    weatherItemResult.setPrecipitation(precipitation);
                }

                if (weather.get("indices") != null) {
                    JSONObject indicesJson = JSON.parseObject(weather.get("indices").toString());
                    Indices indices = new Indices();
                    if (indicesJson.get("earthquake") != null) {
                        indices.setEarthquake(Integer.valueOf(indicesJson.get("earthquake").toString()));
                    }
                    if (indicesJson.get("hail") != null) {
                        indices.setHail(Integer.valueOf(indicesJson.get("hail").toString()));
                    }
                    if (indicesJson.get("hurricane") != null) {
                        indices.setHurricane(Integer.valueOf(indicesJson.get("hurricane").toString()));
                    }
                    if (indicesJson.get("overall") != null) {
                        indices.setOverall(Integer.valueOf(indicesJson.get("overall").toString()));
                    }
                    if (indicesJson.get("ozone") != null) {
                        indices.setOzone(Integer.valueOf(indicesJson.get("ozone").toString()));
                    }
                    if (indicesJson.get("pollution") != null) {
                        indices.setPollution(Integer.valueOf(indicesJson.get("pollution").toString()));
                    }
                    if (indicesJson.get("tornado") != null) {
                        indices.setTornado(Integer.valueOf(indicesJson.get("tornado").toString()));
                    }
                    if (indicesJson.get("wind") != null) {
                        indices.setWind(Integer.valueOf(indicesJson.get("wind").toString()));
                    }
                    weatherItemResult.setIndices(indices);
                }
            }
        }
        return weatherItemResult;
    }


    /**
     * 初始化 group 并转成数组
     *
     * @param group
     * @return
     */
    public String[] initGroups(String group) {
        System.err.println("group:" + group);
        if (StringUtils.isEmpty(group)) {
            group = "zip";
        }
        String[] groups = group.split("\\|");
        Arrays.asList(group).forEach(System.err::println);
        return groups;
    }


    public SchoolItemResult toSchoolItem(HJSchool hjSchool) {

        if (hjSchool != null) {
//            String json = JSON.toJSONString(hjSchool);
//            if (!StringUtils.isEmpty(json)) {
//                SchoolItem schoolItem = JSON.parseObject(json, SchoolItem.class);
//                return schoolItem;
//            }

            List<String> address_list = new ArrayList<>();
            address_list.add(hjSchool.getStreet());
            address_list.add(hjSchool.getCity());
            address_list.add(hjSchool.getState());
            address_list.add(hjSchool.getZip());

            String address = String.join(", ", address_list);
            SchoolItemResult schoolItem = new SchoolItemResult();
            schoolItem.setId(hjSchool.getId());
            schoolItem.setName(hjSchool.getName());
            schoolItem.setAddress(address);
            schoolItem.setType(hjSchool.getAdmission());
            schoolItem.setLat(hjSchool.getLatitude());
            schoolItem.setLng(hjSchool.getLongitude());
            //schoolItem.setPhone(hjSchool.getPhone());
            //schoolItem.setAdmission(hjSchool.getAdmission());
            schoolItem.setGrade(hjSchool.getGradeLow() + "-" + hjSchool.getGradeHigh());
            //schoolItem.setReviewRating(hjSchool.getReviewRating());
            return schoolItem;
        }
        return null;
    }


    public List<DTOInterestPointResult> density(List<DTOInterestPointResult> list, int dist) {
        List<DTOInterestPointResult> rows = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    rows.add(list.get(i));
                } else {
                    Boolean flag = true;
                    Double lat = list.get(i).getLatitude();
                    Double lng = list.get(i).getLongitude();
                    for (DTOInterestPointResult item : rows) {

                        Double lat2 = item.getLatitude();
                        Double lng2 = item.getLongitude();
                        double distance = GeoUtils.getDistance(lat, lng, lat2, lng2);

                        System.err.println("lat:" + lat + "===>>lng:" + lng + "===>>lat2:" + lat2 + "====>>lng2:" + lng2);
                        System.err.println("distance:" + distance);
                        if (distance < dist) {
                            flag = false;
                            break;
                        }
                    }

                    if (flag) {
                        rows.add(list.get(i));
                    }
                }
            }
        }
        return rows;
    }
}
